import pageination from '../../components/pageination/pageination.vue'
export default {
  data() {
    return {
      //数据
      typeThe: '',
      typeName: '',
      viewSpecialist: false,
      obj: {}, // 查看修改obj
      tableData: [], // 列表
      validate: {},
      total: 0, //总条数
      page: 1, //当前页
      size: 10, //每页显示条数
      baseSelect: [], // 基地下拉框
      Specialist: false,
      uploadNum: 0,
      FileUpload: [],
      userId: this.get("userInfo").id,
      attachment: '' ,// 材料路径,
      id: 0,
      baseId: [],
    }
  },
  components: {
    pageination
  },
  created() { //调用方法
    let that = this;
    that.getBase();
  },
  mounted() {
    //加载表单验证
    this.validate = $("#baseAttrForm").validate({
      ignore: [],
      rules: {
        name: { // 名称
          required: true,
          maxlength: 50
        },
        type: { // 名称
          required: true,
        },
        baseId: { // 名称
          required: true,
        },
      },
      messages: {
        name: { // 名称
          required: '请填写材料名称',
          maxlength: '材料名称最多只能输入50个字！'
        },
        type: { // 名称
          required: '请选择材料类别',
        },
        baseId: { // 名称
          required: '请选择基地名称',
        },
      },
    /*  errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });
  },
  methods: { //写函数方法
    getData: function ( ) {
      let that = this;
      let name = $('#searchRoleVal').val();
      if(that.baseId == null || that.baseId.length <= 0){
      }else {
        // 基地材料
        that.$post(
          that.urls.getAttrPage,
          {  "pageNum": that.page,
            "pageSize": that.size,
            "name": name ,"list": that.baseId}
        )
          .then(function(res) {
            if(res.success) {
              that.tableData = res.pageInfo.list;
              that.total = res.pageInfo.total;
              that.page = res.pageInfo.pageNum;

            }
          })
      }

    },
    // 基地数据
    getBase: function ( ) {
      let that = this;
      // 基地展示数据
      that.$post(
        that.urls.basePage,
        {  "list": [1,5,2],    "pageNum": that.page,
          "pageSize": 9999999,
          "userId": that.userId}
      )
        .then(function(res) {
          if(res.success) {

            if (res.pageInfo.list.length > 0 && res.pageInfo.list != null) {
              let select = [];
              let arr = [];
              res.pageInfo.list.forEach(function (e) {
              if (e.status === 1 || e.status === 5 || e.status === 2) {
                arr.push(e.id);
                select.push({"id": e.id, "name": e.name});
              }
            })
            that.baseSelect = select;
            that.baseId = arr;

            setTimeout(function () {
              that.getData();
            },100)

            }

          }
        })
    },
    //页码切换执行方法
    searchData: function(){
      let that = this;

      that.getData();
    },

    //下载资源
    downloadResources: function(filePath) {
      if(filePath != null && filePath != "") {
        window.location.href = this.urls.download + "?path=" + filePath;
      }else {
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
      }
    },
    // 查看
    toView: function (id) {
      let that = this;
      that.delAtts();

      that.$post(
        that.urls.getBaseAttr,
        {"id": id}
      )
        .then(function(res) {
          if(res.success) {
            that.obj = res.data;
           $('#baseId').val(res.data.baseId);
            $('#type').val(res.data.type);

            let enclosureList = [];  // 查看
            enclosureList.push(
           /*   {
              "name": that.getFileName(that.getFileName(res.data.attachment)),}*/
              that.getFileName(res.data.attachment)
            );
            that.FileUpload = enclosureList;


          }
        })
    },
    // del
    delAtt: function (id) {
      let that = this;
      layer.confirm('你确定要删除吗?', {icon: 3, title:'提示'}, function(index){
        if(id){
          that.$post(
            that.urls.removeBaseAttr,
            {"id": id}
          )
            .then(function(res) {
              if(res.success) {
                that.getData();
                layer.msg('成功删除基地材料！', {
                  time: 3000, //3s后自动关闭
                });
              }else {
                layer.msg('删除基地材料失败！', {
                  time: 3000, //3s后自动关闭
                });
              }
            })
        }
      });


    },
    pageFn: function(val, pid) {
      this.page = parseInt(val);
      this.getData();
      //此处调列表数据接口
    },
    /**返回子组件用户选择的一页显示条数**/
    obtainChild: function(data) {
      this.row = data;
    },
    touchOffIdInput(n) {
      this.uploadNum = n;
      if(n == 1) {
        $("#handIdFront").click();
      }
    },
    /**文件上传(多文件)js**/
    idFrontImg: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
            let names = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
              names.push(that.getFileName(fileInfos[i].url));
            }
            that.FileUpload = names;
            console.log(that.FileUpload);
            that.attachment = urls.join(";");
          }
        })
    },
    //新增材料模态框SpecialistBtnView
    SpecialistBtnView: function() {
      let that = this;
      that.validate.resetForm();
      $('#baseId').val('');
      $('#type').val('');
      $('#viewBaseId').val("");
      that.delAtts();
      that.typeName = '新增';
      that.typeThe = '确定';
      that.obj = {};
      that.view = 0;
      that.id = 0;
      that.Specialist = true;
    },

    //新增材料模态框,修改
    SpecialistBtn: function(id, num,bid) {
      let that = this;
      this.validate.resetForm();
      that.delAtts();
      that.obj = {};
      if(id > 0){
        console.log(id)
        $('#viewBaseId').val(bid);
        $('#baseId').val(bid);
        that.id = id;
        that.toView(id);
        if(num){ // 查看
          that.viewSpecialist = true;
        }else {
          that.typeName = '修改';
          that.typeThe = '保存';
          that.Specialist = true;
        }
      }else {
        that.viewSpecialist = true;
      }
    },
    //关闭模态框
    closeModel: function() {
      this.Specialist = false;
this.viewSpecialist = false;
    },
    // 提交
    subBaseAttr: function () {
      let that = this;

      let flg = that.validate.form();
         if(! flg){
           return;
         }
      let   dataObj = that.combinationFormObject(that.validate.currentForm);
      console.log(dataObj)
      if(that.id){
        dataObj.id = that.id;
      }
         // 查重
      that.$post(
        that.urls.queryHeavyAtt,
        dataObj
      )
        .then(function(res) {
          if(res.success) {
            if(res.data){
              layer.msg('材料名称重复，请重新输入!', {
                time: 3000, //3s后自动关闭
              });
            }else {

              let msg = '';
              let errMsg = '';
              let url = '';
              if(that.id){
                url = that.urls.editBaseAttr;
                dataObj.id = that.id;
                msg = '成功修改基地材料！';
                errMsg = '修改基地材料失败！';
              }else{
                url = that.urls.addBaseAttr;
                msg = '成功新增基地材料！';
                errMsg = '新增基地材料失败！';
              }

              that.$post(
                url,
                dataObj
              )
                .then(function(res) {
                  if(res.success) {
                    layer.msg(msg, {
                      time: 3000, //3s后自动关闭
                    });
                    that.closeModel();
                    that.getData();
                  }else {
                    layer.msg(errMsg, {
                      time: 3000, //3s后自动关闭
                    });
                  }
                })

            }
          }
        })
    },
    // del att
    delAtts: function () {
     this.attachment = '';
      this.FileUpload = [];
   //   $('#viewBaseId').val("");
    },
    subStr: function (str) {
      if(str.length > 10){
        let s = str.substr(0, 7);
        return s+ '...';
      }else {
        return str;
      }
    },

    // 跳转到预览
    toSrc: function (url) {
      if(url == null || url == undefined || url == ''){
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.open(this.urls.baseUrl + url);
    }


  }
}
